<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>忘记密码</title>
    <link href="../../css/style.css" rel="stylesheet" type="text/css" />
    <script language="JavaScript" src="../../js/jquery.js" type=""></script>
    <script src="../../js/cloud.js" type="text/javascript"></script>
    <link rel="stylesheet" type="text/css" href="../../css/styles.css">
    <link rel="stylesheet" href="../../css/jigsaw.css">
    <style type="">
        #msg {
            width: 100%;
            line-height: 40px;
            font-size: 14px;
            text-align: center;
        }
    </style>
    <script language="javascript" type="">
        $(function(){
            $('.login-wrap').css({'position':'absolute','left':($(window).width()-525)/2});
            $(window).resize(function(){
                $('.login-wrap').css({'position':'absolute','left':($(window).width()-525)/2});
            })
        });
    </script>
</head>

<body style="background-color:#1c77ac; background-image:url(../../images/light.png); background-repeat:no-repeat; background-position:center top; overflow:hidden;">

<div id="mainBody">
    <div id="cloud1" class="cloud"></div>
    <div id="cloud2" class="cloud"></div>
</div>


<div class="logintop">
    <span>找回密码/更换邮箱/修改密码</span>
    <ul>
        <li><a href="../login.html">登录</a></li>
        <li><a href="upgradeMail.html">更换邮箱</a></li>
    </ul>
</div>

<script src="../../js/jigsaw.js" type=""></script>
<div class="loginbody">

    <span class="systemlogo"></span>

    <div class="login-wrap">
        <div class="login-html">
            <input id="tab-1" type="radio" name="tab" class="sign-in" checked><label for="tab-1" class="tab">通过邮箱重置密码</label>
            <input id="tab-2" type="radio" name="tab" class="sign-up"><label for="tab-2" class="tab">修改密码</label>
            <div class="login-form">
                <div class="sign-in-htm">
                    <div class="group">
                        <label  class="label" id="mailTip">输入您的邮箱</label>
                        <label for="mail"></label><input id="mail" value="" type="text" class="input" oninput="MailRegular('mail')">
                    </div>
                    <div class="group">
                        <label class="label" style="float: left;" id="resetNotice">输入您想要重置的密码</label>
                        <label for="resetPassword"></label><input id="resetPassword" type="password" class="input" oninput="resetPasswordRegular()">
                    </div>

                    <div class="group">
                        <label class="label">验证码</label>
                        <label for="mailCode"></label><input id="mailCode" type="text" style="width: 70%; float: left" class="input" value=""/>
                        <input type="button" class="button" id="getBtn" style="width: 30%; float: right" value="获取验证码" onclick="setTime()">
                    </div>
                    <div class="group">
                        <div class="container">
                            <div id="captcha" style="position: relative" data-type="password"></div>
                            <div id="msg"></div>
                        </div>
                    </div>

                    <div style="height: 2px;margin: 60px 0 8px 0;background: rgba(255,255,255,.2)"></div>

                    <div class="group">
                        <input type="button" class="button" value="重置密码" onclick="resetPasswordSub()">
                    </div>
                    <div class="hr"></div>

                </div>

                <div class="sign-up-htm">
                    <div class="group">
                        <label class="label" id="accountTip">用户名/邮箱</label>
                        <label for="account"></label><input id="account" type="text" class="input" value="" oninput="accountInput()">
                    </div>
                    <div class="group">
                        <label class="label" id="oldPasswordTip">初始密码</label>
                        <label for="oldPassword"></label><input id="oldPassword" type="password" class="input" data-type="password" value="" oninput="oldPasswordInput()">
                    </div>
                    <div class="group">
                        <label class="label"  id="newPasswordTip">修改密码</label>
                        <label for="newPassword"></label><input id="newPassword" type="password" class="input" data-type="password" oninput="newPasswordInput()">
                    </div>
                    <div class="group">
                        <label class="label" id="reNewPasswordTip">确认修改密码</label>
                        <label for="reNewPassword"></label><input id="reNewPassword" type="password" class="input" data-type="password" oninput="reNewPasswordInput()">
                    </div>

                    <div style="height: 2px;margin: 60px 0 8px 0;background: rgba(255,255,255,.2)"></div>
                    <div class="group">
                        <input type="button" class="button" value="确认修改" onclick="PasswordReset()">
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>


<div class="loginbm">版权所有  2021  <a href="" target="_blank">研究开发与实践</a>  XM08-农业大棚信息化</div>
</body>
<script>
    let mailRegular=false;
    function MailRegular(id){
        let re = /^\w+@[a-zA-Z0-9]{2,10}(?:\.[a-z]{2,4}){1,3}$/;
        let text=document.getElementById(id).value;
        if(re.test(text)){
            document.getElementById("mailTip").innerText="邮箱格式合法!";
            mailRegular=true;
        }else{
            document.getElementById("mailTip").innerText="邮箱格式不合法!";
            mailRegular=false;
        }
    }

    let PasswordRegular=false;
    function resetPasswordRegular(){
        let reset=document.getElementById("resetPassword").value;
        if(reset.toString().length>50){
            document.getElementById("resetNotice").innerText="重置密码过长！";
            PasswordRegular=false;
        }else if(reset.toString().length>0){
            document.getElementById("resetNotice").innerText="重置密码合法!";
            PasswordRegular=true;
        }else {
            document.getElementById("resetNotice").innerText="重置密码不可为空!";
            PasswordRegular=false
        }
    }

    // 实现获取验证码的倒计时
    let flagCode=true;
    let backCode;
    let getBtn=document.getElementById("getBtn")
    let countTime=60;
    function setTime(){
        if(mailRegular){
            if(countTime===0){
                getBtn.removeAttribute("disabled");
                getBtn.value="获取验证码";
                countTime=60;
                if(countTime===60){
                    flagCode=true;
                    return;
                }
            }else{
                getBtn.setAttribute("disabled",true);
                getBtn.value="重新发送("+countTime+"s)";
                if(flagCode){
                    if(countTime===60){
                        let url="../../Login_ServletAction";
                        let message={};
                        message.Action="sendCode";
                        message.mail=document.getElementById("mail").value;
                        message.way="通过邮箱重置密码的";
                        $.post(url,message,function (json){
                            if(json.ok===200){
                                backCode=json.code;
                                dstMail=json.mail;
                            }else{
                                alert("验证码发送失败!");
                            }
                        });
                        flagCode=false;
                    }
                }
                countTime--;
            }
            setTimeout(setTime,1000);
        }else{
            alert("邮箱地址不合法!")
        }
    }

    // 通过邮箱重置密码的提交
    function resetPasswordSub(){
        if(!mailRegular || !PasswordRegular){
            alert("请确定邮箱或密码是否合法!");
        }else {
            if(!flagCode && backCode===document.getElementById("resetPassword").value){
                let url="../../Login_ServletAction";
                let message={};
                message.Action="changePassword";
                message.resetPassword=document.getElementById("resetPassword").value;
                message.mail=dstMail;
                message.way="way1";
                console.log(message);
                $.post(url,message,function (json){
                    if(json.ok==200){
                        alert("密码重置成功!")
                    }else if(json.ok==404){
                        alert("该邮箱尚未注册用户!");
                    }else {
                        alert("密码重置失败!");
                    }
                })

            }else{
                alert("验证码已过期或验证码错误!")
            }
        }
        window.location.reload();
    }

    // 修改密码
    let accountRegular=false;
    let oldPasswordRegular=false;
    let newPasswordRegular=false;
    let reNewPasswordRegular=false;

    function accountInput(){
        let account=document.getElementById("account").value;
        if(account.toString().length>30){
            document.getElementById("accountTip").innerText="用户名过长，不合法!";
            accountRegular=false;
        }else if(account.toString().length>0){
            document.getElementById("accountTip").innerText="用户名合法!";
            accountRegular=true;
        }else{
            document.getElementById("accountTip").innerText="用户名不可为空!";
            accountRegular=false;
        }
    }

    function oldPasswordInput(){
        let oldPassword=document.getElementById("oldPassword").value;
        if(oldPassword.toString().length>50){
            document.getElementById("oldPasswordTip").innerText="密码过长，不合法!";
            oldPasswordRegular=false;
        }else if(oldPassword.toString().length>0){
            document.getElementById("oldPasswordTip").innerText="密码合法!";
            oldPasswordRegular=true;
        }else{
            document.getElementById("oldPasswordTip").innerText="密码不可为空!";
            oldPasswordRegular=false;
        }
    }

    function newPasswordInput(){
        let newPassword=document.getElementById("newPassword").value;
        if(newPassword.toString().length>50){
            document.getElementById("newPasswordTip").innerText="密码过长，不合法!";
            newPasswordRegular=false;
        }else if(newPassword.toString().length>0){
            document.getElementById("newPasswordTip").innerText="密码合法!";
            newPasswordRegular=true;
        }else{
            document.getElementById("newPasswordTip").innerText="密码不可为空!";
            newPasswordRegular=false;
        }
    }

    function reNewPasswordInput(){
        let reNewPassword=document.getElementById("reNewPassword").value;
        if(reNewPassword.toString().length>50){
            document.getElementById("reNewPasswordTip").innerText="密码过长，不合法!";
            reNewPasswordRegular=false;
        }else if(reNewPassword.toString().length>0){
            document.getElementById("reNewPassword").innerText="密码合法!";
            reNewPasswordRegular=true;
        }else{
            document.getElementById("reNewPassword").innerText="密码不可为空!";
            reNewPasswordRegular=false;
        }
    }

    function PasswordReset(){
        if(accountRegular && oldPasswordRegular && newPasswordRegular && reNewPasswordRegular){
            let url="../../Login_ServletAction";
            let message={}
            message.Action="changePassword";
            message.account=document.getElementById("account").value;
            message.password=document.getElementById("oldPassword").value;
            message.resetPassword=document.getElementById("reNewPassword").value;
            message.way="way2";
            $.post(url,message,function (json){
                if(json.ok==200){
                    alert("密码修改成功!");
                }else if(json.ok==404){
                    alert("用户或原始密码错误!");
                }else{
                    alert("修改失败!");
                }
            })
        }else{
            alert("请确认所有信息合法!");
        }
        window.location.reload();
    }


</script>

</html>